$(document).ready(function(){
   $('.next').click(function(){
        var error = 0;
        var text_er = '';
        var url = $(location).attr('href');
        var app_id = $('select[name="app"] option:selected').val();

        if(app_id == ''){
            text_er = text_er + "Vui lòng chọn loại ứng dụng!\n";
            error = 1;
        }
        var app_type = $('select[name="type_app"] option:selected').val();
        if(app_type == ''){
            text_er = text_er + "Vui lòng chọn kiểu ứng dụng!";
            error = 1;
        }
        if(error == 1){
            alert(text_er);
            return false;
        }else{
            window.location = url_base +  app_id + '/' + app_type;
            return false;
        }
   }); 
   
   $('input[name="delId"]').click(function(){
        id = $('input[type=checkbox]:checked').first().attr('value');
        if(id != null){ 
            $('.but_tool').removeClass('butx').addClass('but_login').removeAttr('disabled');
        }else{
            $('.but_tool').removeClass('but_login').addClass('butx').attr("disabled",true);
        }
   });
});

function updateStatusAll(status){
    $('input[name="delId"]:checked').each(function(){
        id = $(this).attr('value');
        updateStatus(id, status);
    });
}

function updateStatus(id, status){
    var url = url_base + 'admin/com/'+'ajax/updateStatus';
    $.post(url,{
        id:id,
        status:status,
    },function(data){
        if(data != 'no-data'){
            $('#status_'+id).html(data);
            title = $(data).attr('title');
            $('.status_'+id).text(data);
        } 
        
        if(status == 'del'){
            //$('.status_'+id).parent().remove();
        }
    });
    return false;
}

function add_new(com){
    window.location = url_base + 'admin/' + com + '/them-moi';
}

function com_click(){
    var com_name = '';
    com_id = $('select[name="com"] option:selected').val();
    com_name = $('select[name="com"] option:selected').text();
    if(com_id != ''){
        url = '../admin/action/ajax';
        $.post(url,{com_name:com_name, action:'list_act', app:app,com_id:com_id}, function(data){
            $('select[name="action"] option').remove('option');
            $('select[name="action"]').append(data);
        });
    }
}

function delItem(id, com){
    var url = url_base + 'admin/com/'+'ajax/delitem';
    if (confirm('Bạn có muốn xóa: '+id)){
        $.post(url,{
            id:id,
            com:com
        },function(data){
           $('.status_'+id).parent().remove();
        });
    }
    return false;
}